<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像盒子 -->
      <div class="avatar_box">
        <img src="../assets/头像.jpg" alt=""/>
     </div>
      <!-- 登录表单区域 -->
      <el-form
        label-width="0"
        ref="loginRef"
        class="login_from"
        :model="loginData"
        :rules="loginRules" >
        <!-- 用户名文本框 -->
        <el-form-item prop='username'>
          <el-input v-model="loginData.username"></el-input>
        </el-form-item>
        <!-- 用户名密码框 -->
        <el-form-item prop='password'>
          <el-input placeholder="请输入密码" v-model="loginData.password" show-password @keyup.enter="login"></el-input>
        </el-form-item>
        <!--按钮  -->
        <el-form-item class="btn">
          <el-button type="danger" @click="login" @keyup.enter="login">登录</el-button>
          <el-button type="warning" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  name: 'app',
  methods: {
    login () {
      // 校验是否通过, 通过则提交数据到接口服务器
      this.$refs.loginRef.validate(async isPass => {
        // console.log(isPass)
        if (!isPass) return
        // 异步提交数据并结构赋值保存大对象中包含的data
        const { data } = await this.$http.post('login', this.loginData)
        // 判断响应结果
        if (data.meta.status !== 200) return this.$message.error(data.meta.msg)
        this.$message.success(data.meta.msg)
        // 保存token令牌到本地存储中
        window.sessionStorage.setItem('token', data.data.token)
        // 编程式导航; 修改hash值到token令牌
        this.$router.push('/home')
      })
    },
    reset () {
      this.$refs.loginRef.resetFields()
    }
  },
  data () {
    return {
      loginData: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          {
            required: true,
            message: '请输入登录名',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入登录密码',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 16,
            message: '长度在 3 到 16 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #0a5d42;
  color: #fff;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid rgba(238, 238, 238, 0.5);
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;

  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #fff;
  }
}

.login_from {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;

}

.btn {
  display: flex;
  justify-content: center;
}

</style>
